﻿function Link(from, fromPosIndex, to, toPosIndex) {

    this._domEl = document.createElementNS('http://www.w3.org/2000/svg', "polyline")
    this._domEl.style.strokeWidth = 2;
    this._domEl.style.stroke = "red";
    this._domEl.style.fill = "transparent";
    this._domEl.style.markerEnd = "url(#markerArrow)";

    Element.apply(this, []);

    this._from = from;
    this._to = to;
    this._fromPosIndex = fromPosIndex;
    this._toPosIndex = toPosIndex;
    this._from.addLink(this);
    this._to.addLink(this);

    this.getDomEl = function () {
        return this._domEl;
    }

    this.updateLayout = function () {
        //var fromPos = this._from.getLinkPos()[this._fromPosIndex];
        //var toPos = this._to.getLinkPos()[this._toPosIndex];
        //更新el的位置信息
        var path = this.calcPath();
        this._domEl.setAttribute("points", path);

        if (this.logFlag) {
            worker.postMessage("Create a Line:  path:" + path);
        }
        this.logFlag = false;
    }

    this.calcPath = function () {
        var f = this._from.getDomEl();
        var t = this._to.getDomEl();
        var x1 = this._from.getActualPosition().x;
        var y1 = this._from.getActualPosition().y;
        var w1 = this._from.getSize().w;
        var h1 = this._from.getSize().h;
        var x2 = this._to.getActualPosition().x;
        var y2 = this._to.getActualPosition().y;
        var w2 = this._to.getSize().w;
        var h2 = this._to.getSize().h;

        var point1 = this._from.getCenterPos();
        var point2 = this._to.getCenterPos();

        var points = "";
        //rect1在rect2的正上方
        if (point1.x == point2.x && point1.y < point2.y) {
            points = (x1 + w1 / 2) + " " + (y1 + h1) + "," + (x1 + w1 / 2) + " " + y2;
        }
        //rect1在rect2的左上方
        if (point1.x < point2.x && point1.y < point2.y) {
            points = (x1 + w1 / 2) + " " + (y1 + h1) + "," + (x1 + w1 / 2) + " " + (y2 + h2 / 2) + "," + (x2) + " " + (y2 + h2 / 2);
        }
        //rect1在rect2的右上方
        if (point1.x > point2.x && point1.y < point2.y) {
            points = (x1 + w1 / 2) + " " + (y1 + h1) + "," + (x1 + w1 / 2) + " " + (y2 + h2 / 2) + " " + (x2 + w2) + " " + (y2 + h2 / 2);
        }
        //rect1在rect2的正左边
        if (point1.x < point2.x && point1.y == point2.y) {
            points = (x1 + w1) + " " + (y1 + h1 / 2) + "," + x2 + " " + (y2 + h2 / 2);
        }
        //rect1在rect2的左下方
        if (point1.x < point2.x && point1.y > point2.y) {
            points = (x1 + w1) + " " + (y1 + h1 / 2) + "," + (x2 + w2 / 2) + " " + (y1 + h1 / 2) + "," + (x2 + w2 / 2) + " " + (y2 + h2);
        }
        //rect1在rect2的正下方
        if (point1.x == point2.x && point1.y > point2.y) {
            points = (x1 + w1 / 2) + " " + y1 + "," + (x1 + w1 / 2) + " " + (y2 + h2);
        }
        //rect1在rect2的右下方
        if (point1.x > point2.x && point1.y > point2.y) {
            points = x1 + " " + (y1 + h1 / 2) + "," + (y2 + w2 / 2) + " " + (y1 + h1 / 2) + "," + (x2 + w2 / 2) + " " + (y2 + h2);
        }
        //rect1在rect2的正右方
        if (point1.x > point2.x && point1.y == point2.y) {
            points = x1 + " " + (y1 + h1 / 2) + "," + (x2 + w2) + " " + (y1 + h1 / 2);
        }
        return points;
    }
}